package cn.ccit.DAOImp;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.ccit.DAO.PayOrderRecordDAO;
import cn.ccit.entites.record.PayOrderRecord;
import cn.ccit.util.MyDBUtil;

public class PayOrderRecordDAOimp implements PayOrderRecordDAO{
	private Session session;
	private static final Log log = LogFactory.getLog(PayOrderRecord.class);
	@Override
	public boolean addPayOrderRecord(PayOrderRecord doRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(doRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("save PayOrderRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean removePayOrderRecord(PayOrderRecord doRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(doRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("delete PayOrderRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public boolean changePayOrderRecordInfo(PayOrderRecord doRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(doRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("chanage PayOrderRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}

	@Override
	public List<PayOrderRecord> getSomePayOrderRecords(int start, int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecord as PayOrderRecord order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from PayOrderRecord").uniqueResult();
				if(count>limit){
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomePayOrderRecords fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public PayOrderRecord searchPayOrderRecordByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecord as PayOrderRecord where PayOrderRecord.id=?";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				PayOrderRecord PayOrderRecord=(PayOrderRecord)query.list().get(0);
				session.beginTransaction().commit();
				return PayOrderRecord;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("chanage PayOrderRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<PayOrderRecord> searchPayOrderRecordByYear(String year) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecord as PayOrderRecord where PayOrderRecord.orderYear=?";
				Query query=session.createQuery(hql);
				query.setString(0, year);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord by year fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null; 
	}

	@Override
	public List<PayOrderRecord> searchPayOrderRecordByMonth(String month) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecord as PayOrderRecord where PayOrderRecord.orderMonth=?";
				Query query=session.createQuery(hql);
				query.setString(0, month);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord by month fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

	@Override
	public List<PayOrderRecord> searchPayOrderRecordByDate(String date) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from PayOrderRecord as PayOrderRecord where PayOrderRecord.orderDate=?";
				Query query=session.createQuery(hql);
				query.setString(0, date);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search PayOrderRecord by date fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}

}
